package com.itheima.wms.service.impl;

import com.itheima.wms.mapper.WareHouseMapper;
import com.itheima.wms.pojo.WareHouse;
import com.itheima.wms.pojo.WareHousePageRequest;
import com.itheima.wms.service.WareHouseService;
import com.itheima.wms.utils.PageBean;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class WareHouseServiceImpl implements WareHouseService {

    private final WareHouseMapper wareHouseMapper;

    @Override
    public void addWareHouse(WareHouse wareHouse) {

        wareHouseMapper.addWareHouse(wareHouse);

    }

    @Override
    public PageBean queryWareHouseOnCondationAndPage(WareHousePageRequest wareHousePageRequest) {
        Long total = wareHouseMapper.queryWareHouseCount();

        Integer page = wareHousePageRequest.getCurrent();

        Integer pageSize = wareHousePageRequest.getSize();

        Integer start = (page - 1) * pageSize;

        List<WareHouse> wareHouses = wareHouseMapper.queryWareHouseOnCondation(start, pageSize, wareHousePageRequest);

        PageBean pageBean = new PageBean(wareHouses, total, pageSize);

        return pageBean;
    }

    @Override
    public WareHouse queryWareHouseById(Integer id) {
        WareHouse wareHouse = wareHouseMapper.queryWareHouseById(id);
        return wareHouse;
    }

    @Override
    public void updateWareHouse(WareHouse wareHouse) {

        if (wareHouse.getName() != null) {
            wareHouseMapper.updateWareHouse(wareHouse);
        } else {
            wareHouseMapper.updateWareHouseOnlyStatus(wareHouse);
        }

    }

    @Override
    public void deleteWareHouse(Integer id) {
        wareHouseMapper.deleteWareHouse(id);
    }

    @Override
    public List queryAllWareHousees() {
        List list = wareHouseMapper.queryAllWareHousees();
        return list;
    }

}
